package servlets;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entityJPA.Position;

/**
 * Servlet implementation class SendEmailServlet
 */
@WebServlet("/SendEmailServlet")
public class SendEmailServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SendEmailServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		sendMail(to, p, subjectS, bodyS);
	}
	
	public void sendMail(String to, Position p, String subjectS, String bodyS) {
		System.out.println("");

		String from = "javatrainingmsg@gmail.com";
		String subject = subjectS;
		String body = bodyS;
		Properties props = new Properties();
		props.put("mail.smtp.ssl.trust", "zmr.msg.de");
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "zmr.msg.de");
		props.put("mail.smtp.port", "25");
		Authenticator auth = new SMTPAuthenticator();
		javax.mail.Session session = javax.mail.Session
				.getInstance(props, auth);
		javax.mail.Message msg1 = new MimeMessage(session);
		try {
			msg1.setFrom(new InternetAddress(from));
			msg1.setRecipients(javax.mail.Message.RecipientType.TO,
					InternetAddress.parse(to, false));
			msg1.setSubject(subject);
			msg1.setContent(body, "text/plain");
			Transport.send(msg1);
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}

	}


}
